/*
 * $Id$
 *
 * Copyright ( c ) 2014 XuNuo Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of XuNuo
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with XuNuo Corporation or a XuNuo
 * authorized reseller (the "License Agreement"). XuNuo may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, XuNuo DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. XuNuo DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * XuNuo SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF XuNuo HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. XuNuo' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE XuNuo SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.recipefee.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.th.supcom.pds.common.client.widget.PdsGrid;
import com.th.supcom.pds.common.enums.ControlType;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.recipefee.client.common.RecipeFeeModuleEvents;
import com.th.supcom.pds.sortingdrug.client.model.PdsPrescDetailInfoModel;

/**
 * 收方面板
 * 
 * @author zli
 */
public class RecipePanel extends ContentPanel
{
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";
    private int size = 0;

    CheckBoxSelectionModel <PdsPrescDetailInfoModel> m_checkmodel;

    PdsGrid <PdsPrescDetailInfoModel> m_grid;

    /**
     * 构造函数
     */
    public RecipePanel ()
    {
        initUI ();
        addListeners ();
    }

    private void addListeners ()
    {
        m_checkmodel.addSelectionChangedListener (new SelectionChangedListener <PdsPrescDetailInfoModel> ()
        {
            @Override
            public void selectionChanged (final SelectionChangedEvent <PdsPrescDetailInfoModel> se)
            {
                List <PdsPrescDetailInfoModel> m_overdraftReserveModelModels = m_checkmodel.getSelectedItems ();
                List <PdsPrescDetailInfoModel> models = new ArrayList <PdsPrescDetailInfoModel> ();
                for (PdsPrescDetailInfoModel model : m_overdraftReserveModelModels)
                {
                    if (!model.getControlType ().equals (ControlType.Common.getEnumCode ())
                        && model.getQualifiedType () == null)
                    {
                        models.add (model);
                    }
                }
                m_checkmodel.deselect (models);
            }
        });
        m_checkmodel.addSelectionChangedListener (new SelectionChangedListener <PdsPrescDetailInfoModel> ()
        {

            @Override
            public void selectionChanged (SelectionChangedEvent <PdsPrescDetailInfoModel> se)
            {

                List <PdsPrescDetailInfoModel> models = se.getSelection ();
                PdsPrescDetailInfoModel model = se.getSelectedItem ();
                if (models.size () < size || model == null)
                {
                    size = models.size ();
                    final AppEvent event = new AppEvent (RecipeFeeModuleEvents.CANCEL_DETAIL);
                    Dispatcher.forwardEvent (event);
                    return;
                }
                // if ("1".equals (model.getCheckFlag ()))
                // {
                // m_checkmodel.deselect (model);
                // MessageBox.info ("提示", "该处方需要审核", null);
                // }
                if (models.size () > size)
                {
                    size = models.size ();
                    final AppEvent event = new AppEvent (RecipeFeeModuleEvents.LOAD_DETAIL);
                    Dispatcher.forwardEvent (event);
                }

            }
        });

    }

    private void createGrid ()
    {
        m_checkmodel = new CheckBoxSelectionModel <PdsPrescDetailInfoModel> ();
        m_checkmodel.setSelectionMode (SelectionMode.SIMPLE);
        m_grid = new PdsGrid <PdsPrescDetailInfoModel> ()
        {
            @Override
            public ColumnModel createColumnModel ()
            {
                return getColumnModel ();
            }
        };
        m_grid.getGrid ().setSelectionModel (m_checkmodel);
        m_grid.getGrid ().addPlugin (m_checkmodel);
        add (m_grid);
    }

    public CheckBoxSelectionModel <PdsPrescDetailInfoModel> getCheckmodel ()
    {

        return m_checkmodel;
    }

    /**
     * 创建Grid列信息
     * 
     * @return
     */
    private ColumnModel getColumnModel ()
    {
        final List <ColumnConfig> configs = new ArrayList <ColumnConfig> ();
        ColumnConfig column = null;
        configs.add (m_checkmodel.getColumn ());
        column = new ColumnConfig ("color", "", 20);
        column.setRenderer (new GridCellRenderer <ModelData> ()
        {

            @Override
            public Object render (ModelData model, String property, ColumnData config, int rowIndex, int colIndex,
                                  ListStore <ModelData> store, Grid <ModelData> grid)
            {

                if (model.get ("rcptNo") != null)
                {
                    return "<div style='background-color:red;height:16px' title='已收费'></div>";
                }
                if (!ClientUtils.getOrgModel ().getOrgCode ().equals ((model.get ("orgIdExec"))))
                {
                    return "<div style='background-color:blue;height:16px' title='非本科室'></div>";
                }
                return "";

            }
        });
        configs.add (column);

        column = new ColumnConfig ("prescNo", "处方号", 110);
        configs.add (column);

        column = new ColumnConfig ("orderByDeptName", "开单科室", 75);
        configs.add (column);
        column = new ColumnConfig ("orgIdExecName", "请领药房", 75);
        configs.add (column);

        return new ColumnModel (configs);
    }

    public Grid <PdsPrescDetailInfoModel> getGrid ()
    {

        return m_grid.getGrid ();
    }

    /**
     * 初始化UI
     */
    private void initUI ()
    {
        this.setHeaderVisible (false);
        this.setLayout (new FitLayout ());
        createGrid ();
    }
}
